package cn.piesat.dao;

import cn.piesat.vo.role.RoleUrlProjectionVO;
import cn.piesat.dto.PiesatRoleResourceDTO;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface PiesatRoleResourceDAO extends JpaRepository<PiesatRoleResourceDTO,String> {
    @Query(nativeQuery = true,value="select a.ID,a.ROLE_ID,a.RESOURCE_ID FROM piesat_role_resource_relation a LEFT JOIN piesat_resource_info b ON a.RESOURCE_ID = b.ID WHERE a.ROLE_ID = ?1 AND b.type = ?2 AND b.MENU_ID = ?3")
    List<PiesatRoleResourceDTO> findByRoleIdAndTypeAndMenuId(String id, String type,String menuId);


    void deleteAllByResourceId(String resourceId);
    @Query(nativeQuery = true ,value="select i.RESOURCE AS url,GROUP_CONCAT(ri.MARK) AS markListStr,i.NAME AS name from piesat_resource_info i left join piesat_role_resource_relation r on i.ID = r.RESOURCE_ID left join piesat_role_info ri on ri.ID = r.ROLE_ID where i.TYPE = 'interface' group by i.RESOURCE,i.`NAME`")
    List<RoleUrlProjectionVO> listRoleUrl();

    List<PiesatRoleResourceDTO> findByRoleId(String id);
    @Modifying
    @Query(nativeQuery = true,value="\tdelete FROM piesat_role_resource_relation where role_id = ?1 and RESOURCE_ID in(\n" +
            "\tselect c.resource_id from (\n" +
            "select \n" +
            "\ta.RESOURCE_ID from\tpiesat_role_resource_relation a LEFT JOIN piesat_resource_info b ON a.RESOURCE_ID = b.ID WHERE a.ROLE_ID = ?1 AND b.type = ?2 AND b.MENU_ID = ?3) c)")
    void deleteByRoleIdAndType(String roleId, String type, String menuId);
}
